﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using System.ServiceModel.DomainServices.Client;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Codes;
using LEEAAttendanceControl.Web.Services;

namespace LEEAAttendanceControl.Views
{
    public partial class EmployeeHours : Page
    {
        LEEAContext context = new LEEAContext();

        public EmployeeHours()
        {
            InitializeComponent();

            ChangeEmployees();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            myApp.AddEvent += NewPunchHandler;
            myApp.EditEvent += EditPunchHandler;
            myApp.DeleteEvent += DeletePunchHandler;

            myApp.ChangeEmployeesEvent += ChangeEmployeesHandler;

            ChangeEmployees();
        }

        public void ChangeEmployees()
        {

            HoursSummaryDataSource.QueryParameters.Clear();

            Parameter comParameter = new Parameter();

            comParameter.ParameterName = "companyID";
            comParameter.Value = ((App)Application.Current).CompanyID;
            HoursSummaryDataSource.QueryParameters.Add(comParameter);

            Parameter deParameter = new Parameter();

            deParameter.ParameterName = "departmentID";
            deParameter.Value = ((App)Application.Current).DepartmentID;
            HoursSummaryDataSource.QueryParameters.Add(deParameter);

            Parameter emParameter = new Parameter();

            emParameter.ParameterName = "employeeID";
            emParameter.Value = ((App)Application.Current).EmployeeId;
            HoursSummaryDataSource.QueryParameters.Add(emParameter);

            Parameter pfParameter = new Parameter();

            pfParameter.ParameterName = "periodFrom";
            pfParameter.Value = ((App)Application.Current).PeriodFrom.Date;
            HoursSummaryDataSource.QueryParameters.Add(pfParameter);

            Parameter ptParameter = new Parameter();

            ptParameter.ParameterName = "periodTo";
            ptParameter.Value = ((App)Application.Current).PeriodTo.Date;
            HoursSummaryDataSource.QueryParameters.Add(ptParameter);

            HoursSummaryDataSource.Load();
        }

        private void HoursSummaryDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        public void ChangeEmployeesHandler(object sender, EventArgs e)
        {
            ChangeEmployees();
        }

        public void NewPunchHandler(object sender, EventArgs e)
        {
            HoursSummary summary = EmployeesGrid.SelectedItem as HoursSummary;

            if (summary != null)
            {
                NewPunch newPunch = new NewPunch();

                newPunch.LoadData(summary.EmployeeID);
                newPunch.Closed += NewClosed;

                newPunch.Show();
            }
        }

        public void EditPunchHandler(object sender, EventArgs e)
        {
            Hour hour = PunchesGrid.SelectedItem as Hour;

            HoursSummary summary = EmployeesGrid.SelectedItem as HoursSummary;

            if (hour != null)
            {
                PunchEdit editPunch = new PunchEdit("Edit Punch");

                editPunch.LoadData(hour.HoursID, summary.EmployeeID);
                editPunch.Closed += EditClosed;

                editPunch.Show();
            }
        }

        public void DeletePunchHandler(object sender, EventArgs e)
        {
            if (PunchesGrid.SelectedItem != null)
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.DeleteMessage
                    , ApplicationStrings.Delete, "Ask");

                message.Closed += (s, ee) =>
                {
                    if (message.DialogResult.Value)
                    {
                        Hour hours = PunchesGrid.SelectedItem as Hour;

                        context.DestroyHoursByID(hours.HoursID, OnHoursDeleteCompleted, null);
                    }
                };

                message.Show();
            }
        }

        private void OnHoursDeleteCompleted(InvokeOperation invoke)
        {
            HoursSummary summary = EmployeesGrid.SelectedItem as HoursSummary;

            context.Load(context.GetHoursByEmployeeIDQuery(summary.EmployeeID
                , ((App)Application.Current).PeriodFrom.Date, ((App)Application.Current).PeriodTo.Date)
                , OnHoursDeletedCompleted, null);
        }
        private void OnHoursDeletedCompleted(LoadOperation load)
        {
            PunchesGrid.ItemsSource = load.Entities;
            ChangeEmployees();
        }


        private void EmployeesGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PunchesGrid.ItemsSource = null;

            context.Hours.Clear();

            HoursSummary summary = EmployeesGrid.SelectedItem as HoursSummary;

            if (summary != null)
            {
                context.Load(context.GetHoursByEmployeeIDQuery(summary.EmployeeID
                    , ((App) Application.Current).PeriodFrom.Date, ((App) Application.Current).PeriodTo.Date)
                    , OnHoursLoadCompleted, null);
            }
        }

        private void OnHoursLoadCompleted(LoadOperation load)
        {
            PunchesGrid.ItemsSource = load.Entities;
        }

        protected void NewClosed(object sender, EventArgs e)
        {
            ChangeEmployees();

            App myApp = (App)Application.Current;

            myApp.RefreshStructureTree(sender, e);

            myApp.RefreshStatistics(sender, e);
        }

        protected void EditClosed(object sender, EventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.RefreshStructureTree(sender, e);

            myApp.RefreshStatistics(sender, e);
        }
    }
}
